/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * This file is part of retabel.
 *
 * Copyright (c) 2011 by individual contributors as indicated by the
 * @author tags. See the copyright.txt file in the distribution
 * for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.retabel.service.configuration;

import org.retabel.persistence.dao.configuration.ConfigDao;
import org.retabel.persistence.entity.configuration.ConfigAttribute;
import org.retabel.persistence.entity.configuration.ConfigSet;

import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 * Implementation for {@linkplain SystemConfigAccessor}.
 * <p/>
 * <h3>Extra-Info</h3>
 * Created: 00:25 30.07.11
 *
 * @author Burt Parkers
 * @version 1.0
 */
@Stateless
public class SystemConfigAccessorBean implements SystemConfigAccessor {

    /** Default cache size, if the value cannot found in the persistent configuration */
    static final Integer DEFAULT_DESIGN_CACHE_SIZE = 50;

    /** Name of the config set. */
    static final String CONFIG_SYSTEM_NAME = "systemConfigSet";

    /** Name of the config attribute. */
    static final String ATTR_DESIGN_CACHE = "designCache";

    /** The DAO to access the configuration. */
    @EJB
    private ConfigDao configDao;

    /**
     * Returns the configured size of the design cache.
     *
     * @return returns the configured design cache or a default value.
     */
    @Override
    public int getDesignCacheSize() {
        return getIntegerValue(ATTR_DESIGN_CACHE, DEFAULT_DESIGN_CACHE_SIZE);
    }

    /**
     * Returns the integer value identified by a config attribute name.
     *
     * @param name         the config attribute name.
     * @param defaultValue default value to use if the attribute cannot be found in the database.
     * @return the integer value.
     */
    private Integer getIntegerValue(String name, Integer defaultValue) {
        ConfigSet configSet = configDao.findByName(CONFIG_SYSTEM_NAME);
        Integer value;
        if (configSet != null) {
            ConfigAttribute configAttribute = configSet.getConfigAttribute(name);
            value = Integer.parseInt(configAttribute.getValue());
        } else {
            value = defaultValue;
        }
        return value;
    }

}
